require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var time;
var vm = new Vue({
    el:"#telLogin",
    data:{
        type:9,
        isShow:false,
        isScreen:false,
        mobile:"",
        registrationID:"",
        verificationCode:"",
        verificationCode1:"",
        verificationCode2:"",
        num1:"",
        num2:"",
        num3:"",
        num4:"",
        num5:"",
        num6:"",
        password:"",
        password1:"",
        userInfo:{},
        countdown:30,
        loginUid:"",
        orderNumber:"",
        registerUser:{},
        productId:"",
        number:""   //13 商品详情  14 拼团  15 爆款
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.type=this.searchObject&&this.searchObject.type;
        this.number=this.searchObject&&this.searchObject.number;
        console.log(this.type)
        // this.presendMessage();
        this.$nextTick(function(){
            if(this.type==1||this.type==8){
                $("#telephone").focus();
            }else if(this.type==4){
                $("#psw2").focus();
            }else if(this.type==10){
                console.log(this.type)
                this.mobile=this.searchObject&&this.searchObject.mobile;
                this.tel=this.searchObject&&this.searchObject.tel;
                this.presendMessage();
                this.settime();
            }else if(this.type==11){
                this.loginUid=this.searchObject&&this.searchObject.loginUid;
            }else if(this.type==12){
                this.orderNumber=this.searchObject&&this.searchObject.orderNumber;
            }
            if(this.number==13||this.number==14||this.number==15||this.number==16||this.number==17){
                this.productId=this.searchObject&&this.searchObject.productId;
            }
        });
    },
    methods:{
        //后退
        getBack:function(){
            var that=this;
            if(that.type==8||that.type==1||that.type==4||that.type==10||that.type==11){
                history.go(-1);
            }else if(that.type==9){
                that.type=8;
            }else if(that.type==2||that.type==3){
                that.type=1;
            }
            // else if(that.type==12){
            //     location.href="/purchase/collageAdd?orderNumber="+that.orderNumber;
            // }
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        init:function(){
            this.getData();
            this.getShopData();
            this.getChinaList();
        },
        getClose:function(num){
            var that=this;
            if(that.type==4||that.type==1||that.type==8){
                this.mobile="";
            }
            this.password="";
            this.password1="";
        },
        getClose1:function(){
            this.mobile="";
        },
        getPresend:function(){
            var that=this;
            if(this.type==11){
               that.setUserMobile();
            }else{
                that.num1="";
                that.num2="";
                that.num3="";
                that.num4="";
                that.num5="";
                that.num6="";
                that.verificationCode="";
                that.verificationCode1="";
                that.verificationCode2="";
                that.countdown=30;
                if(that.countdown>0&&that.countdown<30){
                    if(that.type==1){
                        that.type=2
                    }else if(that.type==8){
                        that.type=9;
                    }else if(that.type==4){
                        that.type=5;
                    }
                    return false;
                }else{
                    console.log("ddddddddddddddd")
                    that.presendMessage();
                    console.log("cccccccccccc")

                }
            }
            //this.presendMessage();
        },
        settime:function(){
            var that=this;
            console.log(that.countdown)
            if(that.countdown==0){
                this.countdown=30;
                clearInterval(time);
            }else{
                that.countdown--;
                console.log(this.countdown);
                time=setTimeout(function() {
                    that.settime()
                },1000);
            }
        },
        //预发短信
        presendMessage:function(){
            var that=this;
            console.log(that.mobile);
            if(!that.mobile){
                that.$refs.prompt.toast("手机号不能为空！");
                return false;
            }else{
                if (!/^1[345789]\d{9}$/.test(that.mobile.replace(/^\s+|\s+$/g, ""))) {
                    that.$refs.prompt.toast("请填写正确的手机号码");
                    return false;
                }
            }
            that.registrationID="cms"+guid();
            var data={
                registrationID:that.registrationID
            };
            console.log(data)
            this.ajax({url:"/user/presendMessage",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.settime();
                    that.verificationCode=b.json.data.verificationCode;
                    if(that.type==1||that.type==2){
                        that.sendloginmessage();
                    }else if(that.type==8||that.type==9){
                        that.sendregistermessage();
                    }else if(that.type==4){
                        that.sendforgetmessage();
                    }else if(that.type==10){
                        that.updateregistermessage();
                    }
                }else{
                    vm.$refs.prompt.toast("预发短信失败！");
                }
            });
        },
        //注册短信
        sendregistermessage:function(){
            var that=this,url;
            var data={
                mobile:that.mobile,
                registrationID:that.registrationID,
                verificationCode:that.verificationCode
            };
            console.log(data)
            /*if(that.type==1||that.type==2){
                url="/user/sendloginmessage";
            }else if(that.type==8||that.type==9){
                url="/user/sendregistermessage";
            }else if(that.type==4){
                url="/user/sendforgetmessage"
            }else if(that.type==10){
                url="/happyi/updateregistermessage"
            }*/
            this.ajax({url:"/user/sendregistermessage",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    console.log(b.json.data)
                    that.verificationCode1=b.json.data.verificationCode;
                    //that.register();
                    that.type=9;
                }else{
                    that.mobile="";
                    clearInterval(time);
                    vm.$refs.prompt.toast(b.json.msg||"注册短信失败！");
                }
            });
        },
        //注册
        register:function(){
            var that=this;
            var data={
                mobile:that.mobile,
                registrationID:that.registrationID,
                verificationCode:that.verificationCode1
            };
            console.log(data)
            this.ajax({url:"/user/register",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    clearInterval(time);
                    that.registerUser=b.json.data;
                    that.getIsScreen();
                    //location.href="telLogin?type=1";
                    // that.verificationCode="";
                    // that.verificationCode1="";
                    // that.verificationCode2="";
                }else{
                    vm.$refs.prompt.toast("操作失败！");
                }
            });
        },
        //登录短信
        sendloginmessage:function(){
            var that=this;
            var data={
                mobile:that.mobile,
                registrationID:that.registrationID,
                verificationCode:that.verificationCode
            };
            console.log(data)
            this.ajax({url:"/user/sendloginmessage",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    console.log(b.json.data)
                    that.verificationCode1=b.json.data.verificationCode;
                    that.type=2;
                }else{
                    clearInterval(time);
                    vm.$refs.prompt.toast(b.json.msg||"登录短信失败！");
                }
            });
        },
        //验证码登录
        webloginByCode:function(){
            var that=this;
            var data={
                mobile:that.mobile,
                registrationID:that.registrationID,
                verificationCode:that.verificationCode1
            };
            console.log(data)
            this.ajax({url:"/user/webloginByCode",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    console.log(b.json.data)
                    that.userInfo=b.json.data;
                    that.userInfo.id=that.userInfo.loginUid;
                    // location.href="telLogin?type=1";
                    that.verificationCode="";
                    that.verificationCode1="";
                    that.verificationCode2="";
                    sessionStorage.setItem("userInfo",JSON.stringify(b.json.data));
                    // that.init();
                    that.getData();
                    //that.getShopData();
                    // that.getChinaList();
                    //location.href="/purchase"
                    // if(that.userInfo.birthday&&that.userInfo.sex){
                    //     location.href="/purchase"
                    // }else{
                    //     location.href="/user/perfectUserInfo"
                    // }
                }else{
                    vm.$refs.prompt.toast("操作失败！");
                }
            });
        },
        getPswLogin:function(){
            var that=this;
            clearInterval(time);
            if(that.mobile){
                // that.getShow();
                if (!/^1[345789]\d{9}$/.test(that.mobile.replace(/^\s+|\s+$/g, ""))) {
                    that.getShow();
                    that.mobile="";
                    that.$refs.prompt.toast("请填写正确的手机号码");
                    return false;
                }
                this.isShow=false;
                that.type=3;

                this.$nextTick(function(){
                    $("#telephone1").focus();
                });
            }else{
                // that.getShow();
                this.isShow=false;
                var path="telLogin?type=3";
                console.log(that.productId)
                if(that.number==13||that.number==14||this.number==15||this.number==16||this.number==17){   //商品详情
                    path+="&number="+that.number+"&productId="+that.productId;
                }
                location.href=path;
                this.$nextTick(function(){
                    $("#telephone1").focus();
                });

            }
        },
        getYzm:function(num){
            if(num==7){
                this.password=lost(this.password);
                if(this.password){
                    this.type=num;
                    this.$nextTick(function(){
                        $("#psw4").focus();
                    });
                }else{
                    this.password="";
                }
            }
            if(num==4){
                var path="telLogin?type="+num;
                if(this.number==13||that.number==14||this.number==15||this.number==16||this.number==17){   //商品详情
                    path+="&number="+this.number+"&productId="+this.productId;
                }
                location.href=path;
            }else if(num==2){
                var path="telLogin?type=1";
                if(this.number==13||this.number==14||this.number==15||this.number==16||this.number==17){   //商品详情
                    path+="&number="+this.number+"&productId="+this.productId;
                }
                location.href=path;
                // if(this.mobile){
                //     // that.getShow();
                //     if (!/^1[345789]\d{9}$/.test(that.mobile.replace(/^\s+|\s+$/g, ""))) {
                //         this.getShow();
                //         this.mobile="";
                //         this.$refs.prompt.toast("请填写正确的手机号码");
                //         return false;
                //     }
                //     this.getPresend();
                // }else{
                //     this.$refs.prompt.toast("请填写手机号码");
                //     return false;
                // }

            }else if(num==7){

                // this.$nextTick(function(){
                //     $("#psw4").focus();
                // });
            }
        },
        //通过用户手机号 密码登录
        getLogin:function(){
            var that=this;
            if(!that.mobile){
                that.$refs.prompt.toast("手机号不能为空！");
                return false;
            }else{
                if (!/^1[345789]\d{9}$/.test(that.mobile.replace(/^\s+|\s+$/g, ""))) {
                    that.$refs.prompt.toast("请填写正确的手机号码");
                    return false;
                }
            }
            if(!that.password){
                vm.$refs.prompt.toast("请输入密码");
                return false;
            }
            var data={
                mobile:that.mobile,
                password:that.password
            };
            this.ajax({url:"/user/weblogin",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.userInfo=b.json.data;
                    // that.id=that.userInfo.id;
                    // that.token=that.userInfo.token;
                    that.userInfo.id=that.userInfo.loginUid;
                    sessionStorage.setItem("userInfo",JSON.stringify(b.json.data));
                    //that.init();
                    that.getData();
                    // that.getShopData();
                    // that.getChinaList();
                    // location.href="/purchase";
                    // if(that.userInfo.birthday&&that.userInfo.sex){
                    //     location.href="/purchase"
                    // }else{
                    //     location.href="/user/perfectUserInfo"
                    // }
                    //console.log(that.userInfo)
                }else{
                    vm.$refs.prompt.toast(b.json.msg||"登录失败！");
                }
            });
        },
        getShow:function(){
            this.isShow=!this.isShow;
        },
        getIsScreen:function(type){
            this.isScreen=!this.isScreen;
            if(type){
                var path="telLogin?type=1";
                if(this.number==13||this.number==14||this.number==15||this.number==16||this.number==17){   //商品详情
                    path+="&number="+this.number+"&productId="+this.productId;
                }
                location.href=path;
                //location.href="telLogin?type=1"
            }
        },
        //设置-修改手机号码短信
        updateregistermessage:function(){
            var that=this;
            var data={
                ymobile:that.tel,
                mobile:that.mobile,
                registrationID:that.registrationID,
                verificationCode:that.verificationCode
            };
            console.log(data)
            this.ajax({url:"/happyi/updateregistermessage",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    console.log(b.json.data)
                    that.verificationCode1=b.json.data.verificationCode;
                }else{
                    clearInterval(time);
                    vm.$refs.prompt.toast(b.json.msg||"获取短信失败！");
                }
            });
        },
        //设置-修改手机号码
        updateMobile:function(){
            var that=this;
            var data={
                ymobile:that.tel,
                mobile:that.mobile,
                registrationID:that.registrationID,
                verificationCode:that.verificationCode1
            };
            console.log(data)
            this.ajax({url:"/happyi/updateMobile",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    location.href="/happyi/security"
                }else{
                    clearInterval(time);
                    vm.$refs.prompt.toast(b.json.msg||"获取短信失败！");
                }
            });
        },
        //设置-手机号码
        setUserMobile:function(){
            var that=this;
            var data={
                loginUid:that.loginUid,
                mobile:that.mobile
            };
            console.log(data)
            this.ajax({url:"/happyi/setUserMobile",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    location.href="/happyi/security"
                }else{
                    vm.$refs.prompt.toast("设置失败！");
                }
            });
        },
        //忘记密码短信
        sendforgetmessage:function(){
            var that=this;
            var data={
                mobile:that.mobile,
                registrationID:that.registrationID,
                verificationCode:that.verificationCode
            };
            console.log(data)
            this.ajax({url:"/user/sendforgetmessage",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    console.log(b.json.data)
                    that.verificationCode1=b.json.data.verificationCode;
                    that.type=5;
                }else{
                    clearInterval(time);
                    vm.$refs.prompt.toast(b.json.msg||"获取短信失败！");
                }
            });
        },
        //设置忘记密码
        setForgetCode:function(){
            var that=this;
            if(that.password!=that.password1){
                vm.$refs.prompt.toast("两次密码不一致，请重新输入！");
                that.password="";
                that.password1="";
                that.type=6;
                return false;
            }
            var data={
                mobile:that.mobile,
                registrationID:that.registrationID,
                password:that.password
            };
            console.log(data)
            this.ajax({url:"/user/setForgetCode",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    location.href="telLogin?type=1";
                }else{
                    vm.$refs.prompt.toast("操作失败！");
                }
            });
        },
        getNext:function(){
            if(this.number==13){   //商品详情
                location.href="/purchase/goodsDetails?type=1&productId="+this.productId;
            }else if(this.number==14){   //拼团
                location.href="/purchase/collageAdd?orderNumber="+this.productId;
            }else if(this.number==15){   //爆款
                location.href="/purchase/burstingInfo?type=3&orderNumber="+this.productId;
            }else if(this.number==16){   //个人商品
                location.href="/userGoodsInfo?productId="+this.productId;
            }else if(this.number==17){   //游记
                location.href="/cityAd/travelsInfo?cityAdvId="+this.productId;
            }else{
                location.href="/purchase";
            }
        },
        //初始化数据
        getData:function(){
            var that=this;
            var data={
                loginUid:that.userInfo.loginUid,
                version:"1"
            };
            this.ajax({url:"/user/data",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    sessionStorage.setItem("data",JSON.stringify(b.json.data));
                    that.getShopData();
                    console.log(b.json.data)
                }else{
                    vm.$refs.prompt.toast("初始化数据失败！")
                }
            });
        },
        //初始化电商数据
        getShopData:function(){
            var that=this;
            var data={
                loginUid:that.userInfo.loginUid,
                version:"1"
            };
            this.ajax({url:"/user/shopData",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    sessionStorage.setItem("shopData",JSON.stringify(b.json.data));
                    console.log(b.json.data)
                    that.getChinaList();
                }else{
                    vm.$refs.prompt.toast("初始化电商数据失败！")
                }
            });
        },
        //获取中国省市区列表
        getChinaList:function(){
            var that=this;
            this.ajax({url:"/selfMedia/getChinaList",data:{version:1},dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.chinaList=b.json.data;
                    sessionStorage.setItem("chinaList",JSON.stringify(that.chinaList));
                    that.getNext();
                    //console.log(that.chinaList)
                }else{
                    vm.$refs.prompt.toast("获取省市列表失败！")
                }
            });
        }
    },
    watch:{
        num1:function(){
            var that=this;
            $("#code2").focus();
            that.verificationCode2=that.num1+that.num2+that.num3+that.num4+that.num5+that.num6;
        },
        num2:function(){
            var that=this;
            $("#code3").focus();
            that.verificationCode2=that.num1+that.num2+that.num3+that.num4+that.num5+that.num6;
        },
        num3:function(){
            var that=this;
            $("#code4").focus();
            that.verificationCode2=that.num1+that.num2+that.num3+that.num4+that.num5+that.num6;
        },
        num4:function(){
            var that=this;
            $("#code5").focus();
            that.verificationCode2=that.num1+that.num2+that.num3+that.num4+that.num5+that.num6;
        },
        num5:function(){
            var that=this;
            $("#code6").focus();
            that.verificationCode2=that.num1+that.num2+that.num3+that.num4+that.num5+that.num6;
        },
        num6:function(){
            var that=this;
            that.verificationCode2=that.num1+that.num2+that.num3+that.num4+that.num5+that.num6;
            if(!that.num6){
                $("#code1").focus();
            }
        },
        verificationCode1:function(){
            this.$nextTick(function(){
                $("#code1").focus();
            });
        },
        verificationCode2:function(){
            var that=this;
            console.log(that.verificationCode2)
            console.log(that.verificationCode1)
            console.log(that.verificationCode2.split(""))
            if(that.verificationCode2.split("").length==6){
                if(that.verificationCode1==that.verificationCode2){
                    if(that.type==8||that.type==9){
                        that.register();
                    }else if(that.type==1||that.type==2){
                        that.webloginByCode();
                    }else if(that.type==5){
                        that.type=6;
                        $("#psw3").focus();
                    }else if(that.type==10){
                        that.updateMobile();
                    }
                }else{
                    vm.$refs.prompt.toast("验证码不正确！");
                    that.num1="";
                    that.num2="";
                    that.num3="";
                    that.num4="";
                    that.num5="";
                    that.num6="";
                    $("#code1").focus();
                }
            }
        }
    }
});
//用于生成uuid
function S4() {
    return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function guid() {
    return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
function lost(value){
    var patrn =/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/;
    value=value.replace(/(^\s*)|(\s*$)/g,"");
    if(!(patrn.test(value))){
        vm.$refs.prompt.toast("密码格式不正确！");
    }else{
        return value;
    }
}